package org.gnome.wsgalaxy.gnodroid;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.net.ServerSocket;
import java.net.Socket;

public class MainService extends Service implements Runnable {

    private ServiceManager m_serviceManager = new ServiceManager();

    @Override
    public void
    onCreate()
    {
        Thread thread = new Thread(this);
        thread.start();
        Global.SERVICE_CONTEXT = this;
    }

    @Override
    public IBinder
    onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void
    onDestroy()
    {
        Global.SERVICE_CONTEXT = null;
    }

    public void
    run()
    {
        __runInBackground();
    }
/*
    private void
    __processSocket(Socket socket)
    {}
*/
    private void
    __runInBackground() {
        Log.d("gnd", "run in background");

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(Global.PORT);
            Socket socket = null;
            while (true) {
                socket = serverSocket.accept();
                //__processSocket(socket);
                Log.d("gnd", "accept socket");
                m_serviceManager.startService(socket);
            }
        } catch (Exception e) {
            Log.d("gnd", "server socket set up failed");
        }


    }


}
